//
//  RestaurantHomeViewController.m
//  VietQuan
//
//  Created by hainguyen on 11/13/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "RestaurantHomeViewController.h"
#import "Restaurant.h"
#import "RestaurantExtension.h"
#import "ItemView.h"
#import "RestaurantImage.h"
#import "RestaurantImageExtension.h"
#import "UIImageExtension.h"
 
@implementation RestaurantHomeViewController

@synthesize scrollView;
@synthesize restArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
	[self.view addSubview:scrollView];
	restArray = [[NSMutableArray alloc] initWithArray:[Restaurant getRestaurants]];
	CGFloat y = 0.0;
	CGFloat contentSize = [restArray count] * 80 + 43;
	for (int i = 0; i < [restArray count]; i++) {
		ItemView *restView = [[[NSBundle mainBundle] loadNibNamed:@"ItemView" owner:self options:nil] lastObject];
		[self.scrollView addSubview:restView];
		[restView setFrame:CGRectMake(0, y, 320, 80)];
		Restaurant *restaurant = [restArray objectAtIndex:i];
		restView.titleLabel.text = restaurant.name;
		restView.summaryLabel.text = restaurant.streetName;
		RestaurantImage *restaurantImage = [RestaurantImage getRestaurantImagesWithRestaurant:[restaurant.id intValue]];
		UIImage *image = [UIImage imageWithData:restaurantImage.data];
		[image scaleToSize:CGSizeMake(96, 77)];
		UIImage *imageRoundCorner = [UIImage makeRoundCornersImage:image :10 :10]; 
		restView.thumbnailImage.image = imageRoundCorner;
		y = y + 80;		
	}
	[scrollView setContentSize:CGSizeMake(320, contentSize)];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc
{
	self.scrollView = nil;
	[restArray release];
	[super dealloc];
}

@end
